package com.zmx.patientend.view.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.OrderItemAdapter;
import com.zmx.patientend.fragment.GraphicPhysicianFragment;
import com.zmx.patientend.fragment.PrescribingPhysicianFragment;
import com.zmx.patientend.fragment.VoicePhysicianFragment;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.PatientOrder;
import com.zmx.patientend.utils.AllUtil;
import com.zmx.patientend.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 时间：2020/4/30 15:08
 * 功能：我的问诊
 */
public class MePhysicianActivity extends BaseActivity {

    static final int NUM_ITEMS = 2;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private String[] strings = new String[]{"在线问诊","专家团问诊"};

//    private RecyclerView reView;
//    private SmartRefreshLayout refreshLayout;
//    private OrderItemAdapter adapter;
//    private List<PatientOrder> lists;
//    private View mEmptyLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_me_physician;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);

        fragmentList.add(new GraphicPhysicianFragment());
        fragmentList.add(new PrescribingPhysicianFragment());

        TabLayout tab_layout = findViewById(R.id.tab_layout);
        ViewPager viewPager = findViewById(R.id.viewPager);
        MyAdapter fragmentAdater = new  MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentAdater);
        tab_layout.setupWithViewPager(viewPager);


//        mEmptyLayout = findViewById(R.id.empty);
//        reView = findViewById(R.id.reView);
//        reView.setLayoutManager(new LinearLayoutManager(mActivity));
//        adapter = new OrderItemAdapter(mActivity);
//        reView.setAdapter(adapter);
//        lists = new ArrayList<>();
//        adapter.setOnItemClickListener(new OnItemClickListener() {
//            @Override
//            protected void onItemClick(View view, int listPosition) {
//
//                Intent intent = new Intent(mActivity, OrderDetailsActivity.class);
//                intent.putExtra("ask_id", lists.get(listPosition).getId() + "");
//                intent.putExtra("patient_id", lists.get(listPosition).getPatient_id() + "");
//                intent.putExtra("hosp_name", lists.get(listPosition).getHosp_name() + "");
//                intent.putExtra("title_name", lists.get(listPosition).getTitle_name() + "");
//                intent.putExtra("class_name", lists.get(listPosition).getClass_name() + "");
//                intent.putExtra("doctor_name", lists.get(listPosition).getName() + "");
//                intent.putExtra("doctor_head", lists.get(listPosition).getAvatar() + "");
//                startActivity(intent);
//
//            }
//        });
//
//        // 开启自动加载功能
//        refreshLayout = findViewById(R.id.refreshLayout);
//        refreshLayout.setEnableAutoLoadMore(true);
//        refreshLayout.setOnRefreshListener(refreshLayout -> {
//
//            getData();
//        });
//
//        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
//
//            handler.sendEmptyMessage(3);
//
//        });
//
//        //触发自动刷新
//        refreshLayout.autoRefresh();


    }

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return strings[position];
        }
    }
//    @SuppressLint("HandlerLeak")
//    private Handler handler = new Handler() {
//
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            super.handleMessage(msg);
//
//            switch (msg.what) {
//
//                case 1:
//
//                    lists.clear();
//                    try {
//
//                        JSONObject object = new JSONObject(msg.obj.toString());
//                        if (object.getInt("code") == 1) {
//
//                            JSONArray array = new JSONArray(object.getString("data"));
//                            Gson gson = new Gson();
//                            for (int i = 0; i < array.length(); i++) {
//
//                                JSONObject jsonObject = array.getJSONObject(i);
//                                PatientOrder order = gson.fromJson(jsonObject.toString(), PatientOrder.class);
//                                lists.add(order);
//
//                            }
//
//                        }
//
//                        adapter.setListAndNotifyDataSetChanged(lists);//就是设置list然后刷新
//                        handler.sendEmptyMessage(2);
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                    break;
//
//                case 2:
//                    //刷新加载
//                    refreshLayout.finishRefresh(true);
//                    if (lists.size() > 0) {
//
//                        mEmptyLayout.setVisibility(View.GONE);
//
//                    } else {
//
//                        mEmptyLayout.setVisibility(View.VISIBLE);
//
//                    }
//                    break;
//
//                case 3:
//                    //刷新加载
//                    refreshLayout.finishLoadMore(true);
//
//                    break;
//
//            }
//
//        }
//    };
//
//    /**
//     * 获取所有提问
//     */
//    public void getData() {
//
//        List<String> ls = new ArrayList<>();
//        ls.add("10");
//        ls.add("1");
//        ls.add("1");
//        //请求网络
//        Map<String, Object> params = new HashMap<String, Object>();
//        params.put("sign", AllUtil.md5(sing(ls)));
//        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
//        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
//        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
//        params.put("cmd", "patientAskList");
//        params.put("data[type]", "1");
//        params.put("data[row]", "1");
//        params.put("data[number]", "10");
//        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 1, 404);
//
//    }
//
//    //签名
//    public String sing(List<String> list) {
//
//        StringBuilder requestparams = new StringBuilder();
//
//        requestparams.append(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, "") +
//                MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.role, "") +
//                System.currentTimeMillis() / 1000 + MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.password, ""));
//
//        if (list != null) {
//            for (String s : list) {
//                requestparams.append(s);
//            }
//        }
//        return requestparams.toString();
//    }

}
